/**
 * DefaultCatalog.java 7 avr. 2006
 * 
 * Copyright (c) 2006 Emmanuel PUYBARET / eTeks <info@eteks.com>. All Rights
 * Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.windu2b.jcaddie.io;

import java.net.*;
import java.util.*;

import org.windu2b.jcaddie.model.*;
import org.windu2b.jcaddie.model.UserPreferences.*;
import org.windu2b.jcaddie.tools.*;

/**
 * Product default catalog read from localized resources.
 * 
 * @author Emmanuel Puybaret, windu.2b
 */
public class DefaultCatalog extends Catalog
{
	/**
	 * Creates a default catalog read from resources.
	 */
	public DefaultCatalog()
	{
		ResourceBundle resource = ResourceBundle
		        .getBundle( DefaultCatalog.class.getName() );

		for ( int i = 1;; i++ )
		{
			String name = null;
			try
			{
				name = resource.getString( "name#" + i );
			}
			catch ( MissingResourceException ex )
			{
				break;
			}
			Content icon = getContent( resource, "icon#" + i );
			String category = resource.getString( "category#" + i );
			String id = resource.getString( "id#" + i );
			Float price = Float.valueOf( resource.getString( "prix#" + i ) );
			Float poids = Float.valueOf( resource.getString( "poids#" + i ) );
			String unite = resource.getString( "unite#" + i );

			Category cat = new Category( category, id );
			CatalogPieceOfProduct produit = new CatalogPieceOfProduct( name,
			        icon, cat, price, poids, Unit.valueOf( unite.trim()
			                .toUpperCase() ) );
			add( cat, produit );
		}
	}




	/**
	 * Returns a valid URLContent instance from the resource file value of key.
	 * 
	 * @param resource
	 *            a resource bundle
	 * @param key
	 *            the key of a resource file
	 * @throws IllegalArgumentException
	 *             if the file value doesn't match a valid resource.
	 */
	private Content getContent( ResourceBundle resource, String key )
	{
		String file = resource.getString( key );
		URL url = getClass().getResource( file );

		if ( url == null )
			throw new IllegalArgumentException( "unknown resource " + file );
		else return new URLContent( url );
	}
}